package com.devspark.screenshooter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.RemoteWebDriver;

public class ScreenShooter {
	
	private UrlManager urlManager;
	private Profile profile;
	
	public ScreenShooter(UrlManager urlManager, Profile profile) {
		super();
		this.urlManager = urlManager;
		this.profile = profile;
	}
	
	
	public void execute(){
		Vector<RemoteWebDriver> navigators = new Vector<RemoteWebDriver>();
		
		if (profile.getNavigators().contains("firefox"))
			navigators.add(new FirefoxDriver());
		
		for(RemoteWebDriver navigator : navigators){ 
			int urlNumber = 0;
    		for(String url : urlManager.getUrls()){	
    			navigator.get(url);
    			File scrFile = ((TakesScreenshot) navigator).getScreenshotAs(OutputType.FILE);
    			String fileName = "."+numberFormat(urlNumber,3) + navigator.toString() + ".png";
                try {
					FileUtils.copyFile(scrFile, new File(fileName));
				} catch (IOException e) {
					e.printStackTrace();
				}
                urlNumber++;
    		}
    		navigator.close();
		}
	}
	
	private String numberFormat(int n,int length){
		String r = new Integer(n).toString();
		for (int i=r.length();i<length;i++)
			r= "0" + r;
		return r;
	}
	
	
}
